<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
  import DialogEditor from '@/components/DialogEditor'
  import dialogMixins from '@/utils/commonDialogMixins'
  import {mRequest} from '@/utils/request'

  export default {
    mixins: [dialogMixins],
    components: {DialogEditor},
    data() {
      return {
        dataFormConfig: {
          orderDetailId: {
            label: '订单编号',
            type: 'list',
            list: [],
            placeholder: '请输入订单编号',
            selectCallback: this.orderSelectCallback
          },
          productId: {
            label: '产品名称',
            type: 'list',
            list: [],
            disabled: true,
            placeholder: '请选择产品名称'
          },
          productWeight: {
            label: '标准克重',
            disabled: true,
            placeholder: '请输入标准克重',
            selectCallback: this.numberSelectCallback
          },
          unitPrice: {
            label: '单价（光瓶）',
            type: 'number',
            max: 10000
          },
          orderProductNumber: {
            label: '订单数量',
            disabled: true,
            default: 0
          },
          putInStorageNumber: {
            label: '库存数量',
            disabled: true,
            default: 0
          },
          leaveStorageNumber: {
            label: '已发数量',
            disabled: true,
            default: 0
          },
          deliveryNumber: {
            label: '发货数量',
            type: 'number',
            max: 10000000,
            default: 0,
            selectCallback: this.numberSelectCallback
          },
          deliveryType: {
            label: '数量类型',
            type: 'radio',
            radioBorder: false,
            radioList: [
              {text: '按数量', label: 0},
              {text: '全发', label: 1},

            ],
            default: 0
          },
          deliveryAddress: {
            label: '收货地址',
            inputType: 'textarea',
            placeholder: '请输入收货地址'
          },
          weightInfo:{
            label: '重量(吨)',
            max: 10000000,
            default: 0
          },
          status: {
            label: '状态',
            type: 'switch',
            activeValue: 1,
            inactiveValue: 0,
            default: 1
          }
        }
      }
    },
    methods: {
      init(id, property, cb, submitAtFinish = true) {
        this.dataForm.id = id || 0
        this.initOrderDetailList('orderDetailId')
        this.initProductInfoList('productId')
        this.$refs.dialog.initDialog({
          submitAtFinish,
          dataForm: this.dataForm,
          config: this.dataFormConfig,
          dataRule: this.getNeedDataRule('orderDetailId', 'productId','productWeight','deliveryNumber', 'unitPrice','status'),
          initApi: 'consignApi/consignNoteDetailInfo',
          saveApi: 'consignApi/saveConsignNoteDetail',
          updateApi: 'consignApi/updateConsignNoteDetail',
          objectName: 'consignNoteDetail',
          submitCallback: cb
        })
        this.$nextTick(() => {
          if (!this.R.isNil(property)) {
            Object.keys(property).forEach((key) => {
              this.dataForm[key] = property[key]
            })
          }
        })
      },

      orderSelectHandle(orderId, cb) {
        if (this.R.isNil(orderId) || this.R.isEmpty(orderId)) {
          return
        }
        mRequest('orderApi/orderDetailInfo', {id: orderId}, ({data}) => {
          if (!(cb instanceof Function)) {
            return
          }
          return cb(data)
        })
      },
      orderSelectCallback() {
        this.orderSelectHandle(this.dataForm.orderDetailId, (data) => {
          this.dataForm['productId'] = this.R.pathOr(0, ['orderDetail', 'productId'], data)
          this.dataForm['productWeight'] = this.R.pathOr(0, ['orderDetail', 'productWeight'], data)
          this.dataForm['unitPrice'] = this.R.pathOr(0, ['orderDetail', 'unitPrice'], data) == 0 ? undefined : this.R.pathOr(0, ['orderDetail', 'unitPrice'], data)
          this.dataForm['orderProductNumber'] = this.R.pathOr(0, ['orderDetail', 'orderProductNumber'], data)
        })
      },
      numberSelectCallback() {
        const productWeight = this.R.propOr(0, 'productWeight', this.dataForm)
        const deliveryNumber = this.R.propOr(0, 'deliveryNumber', this.dataForm)

        this.dataForm.weightInfo = productWeight * deliveryNumber/1000000
      },
    }
  }
</script>

